package org.sky.sys.controller;

import org.apache.commons.lang3.StringUtils;
import org.sky.exception.ServiceException;
import org.sky.sys.entity.SysOrganType;
import org.sky.sys.service.SysOrganTypeService;
import org.sky.sys.util.CriteriaUtils;
import org.sky.sys.util.RequestUtils;
import org.sky.sys.util.page.PageListData;
import org.sky.util.JsonUtils;
import org.sky.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author weifx
 * @Title: SysOrganTypeController
 * @ProjectName springboot-platform
 * @Description: TODO{组织机构类型}
 * @date 2019/3/7 12:29
 * @Version 1.0
 */
@Controller
@RequestMapping(value="/sys/SysOrganType")
public class SysOrganTypeController {
    @Autowired
    private SysOrganTypeService sysOrganTypeService;
    /**
     * 打开页面
     * @return
     */
    @GetMapping(value="/initSysOrganTypeListPage")
    public String initSysOrganTypeListPage(){
        return "sys/organ/sysorgantypelist";
    }

    /**
     * 分页查询
     * @param request
     * @return
     */
    @PostMapping(value = "/selectSysOrganTypeByPage",produces = "application/json;charset=UTF-8")
    public @ResponseBody
    String selectSysOrganTypeByPage(HttpServletRequest request){
        String filter = request.getParameter("filter");
        Map filterMap = JsonUtils.json2map(filter);
        String sortfield=request.getParameter("sortfield");
        int pageNum=Integer.parseInt(request.getParameter("page"));//当前第几页
        int pageSize=Integer.parseInt(request.getParameter("rows"));//每页显示条数
        //组织查询条件
        Example example= new Example(SysOrganType.class);
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.initFilter(criteria, filterMap);
        if(!StringUtils.isBlank(sortfield)) {
            example.setOrderByClause(sortfield);
        }
        PageListData pageData = sysOrganTypeService.selectSysOrganTypeByPage(pageNum,pageSize,example);
        return JsonUtils.obj2json(pageData);
    }

    /**
     * 打开新增页面
     * @return
     */
    @GetMapping(value="/initSysOrganTypeAddPage")
    public String initSysOrganTypeAddPage(){
        return "sys/organ/sysorgantypeedit";
    }
    /**
     * 打开编辑页面
     * @return
     */
    @GetMapping(value="/initSysOrganTypeEditPage")
    public String initSysOrganTypeEditPage(){
        return "sys/organ/sysorgantypeedit";
    }

    /**
     * 保存{添加/修改}机构类型
     * @param request
     * @return
     */
    @PostMapping(value = "/saveAddEditSysOrganType",produces = "application/json;charset=UTF-8")
    public @ResponseBody String saveAddEditSysOrganType(HttpServletRequest request){
        Result result = null;
        try {
            SysOrganType sysOrganType = RequestUtils.getEntityBean(request, SysOrganType.class);
            sysOrganTypeService.saveAddEditSysOrganType(sysOrganType);
            result = Result.success("机构类型保存成功",null,null);
        }catch (ServiceException e){
            result = Result.success("机构类型保存失败",e.getMessage(),null);
        }
        return JsonUtils.obj2json(result);
    }

    /**
     * 根据Id查询机构类型
     * @param id
     * @return
     */
    @GetMapping(value = "/getSysOrganTypeById/{id}",produces = "application/json;charset=UTF-8")
    public @ResponseBody String getSysOrganTypeById(
            @PathVariable Long id){
        SysOrganType bean = sysOrganTypeService.getSysOrganTypeById(id);
        return JsonUtils.obj2json(bean);
    }

    /**
     * 删除机构类型
     * @param request
     * @return
     */
    @PostMapping(value = "/delSysOrganType",produces = "application/json;charset=UTF-8")
    public @ResponseBody String delSysOrganType(
            HttpServletRequest request){
        Result result= null;
        try {
            String delRows=request.getParameter("delRows");
            List<SysOrganType> de=JsonUtils.json2list(delRows, SysOrganType.class);
            sysOrganTypeService.delSysOrganTypeById(de);
            result = Result.success("删除成功",null,null);
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            result = Result.success("删除失败",e.getMessage(),null);
        }
        return JsonUtils.obj2json(result);
    }
}
